package com.budweiser.user.dao;

import com.budweiser.model.user.Role;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * 用户角色关系<br>
 * 用户和角色是多对多关系，sys_role_user是中间表
 *
 * @author 刘恒福
 */
@Mapper
public interface UserRoleDao {
	/***
	 * 根据用户ID查询用户角色结合
	 * 
	 * @param userId
	 * @return
	 */
	@Select("select r.* from sys_role_user ru inner join sys_role r on r.id = ru.roleId where ru.userId = #{userId}")
	Set<Role> findRolesByUserId(Long userId);

	/***
	 * 根据用户查询角色id
	 * 
	 * @param userid
	 * @return
	 */
	@Select("select r.id as id,r.code as code,r.`name` as name ,IF(u.userId is null ,false,true) as deleteStatus from sys_role r left join sys_role_user u on r.id = u.roleId and u.userId = #{userid}")
	List<Role> findUserRole(Long userid);

	/***
	 * 根据用户id，角色Id删除用户角色表
	 * 
	 * @param userid
	 * @param roleid
	 */
	void delete(@Param("userid") Long userid, @Param("roleid") Long roleid);

	/***
	 * 根据用户id，角色Id新增用户角色表
	 * 
	 * @param userid
	 * @param roleid
	 */
	@Insert("insert into sys_role_user(userId, roleId)values(#{userid}, #{roleid})")
	void save(@Param("userid") Long userid, @Param("roleid") Long roleid);
}
